package controllers

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"cmt1-admin/lib/request"
)

type Mail struct {
	Type    int64    `form:"type" json:"type"`
	Uids    []string `form:"uids" json:"uids"`
	Title   string   `form:"title" json:"title"`
	Content string   `form:"content" json:"content"`
	Res     string   `form:"res" json:"res"`
}

func SendMail(c *gin.Context) {
	umail := new(Mail)
	if err := c.ShouldBindJSON(umail); err != nil {
		handleErr(c, 500, err.Error())
		return
	}
	if umail.Title == "" || umail.Content == "" {
		handleErr(c, 400, "邮件标题或内容为空")
		return
	}
	if umail.Type == 0 {
		handleErr(c, 400, "无效的邮件类型")
		return
	}
	mdata := make(map[string]interface{}, 0)
	mdata["uids"] = umail.Uids
	mdata["type"] = umail.Type
	mdata["title"] = umail.Title
	mdata["content"] = umail.Content
	mdata["res"] = umail.Res
	res, err := request.Post("mail", mdata)
	if err != nil {
		handleErr(c, 401, err.Error())
		return
	}
	c.Writer.Header().Set("content-type", "application/json")
	c.String(http.StatusOK, string(res))
}
